//
//  HDRootVC.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/4/8.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HDRootVC.h"
#import "HDBottomBarView.h"
#import "HDSIMJudge.h"

@interface HDRootVC ()<HDBottomBarViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic, strong) NSArray *imgNameArray;
@property (nonatomic, strong) NSArray *titleArray;
@property (weak, nonatomic) IBOutlet UIImageView *routeTipImgView;

@end

@implementation HDRootVC

#pragma mark
#pragma mark -------- Init Methods --------

- (void)viewDidLoad {
    [super viewDidLoad];
    [self barInit];
    [self tableviewInit];
    
    [self userIDRequestAction];
    [self tipImgViewInit];
}

- (void)barInit
{
    HDBottomBarView *bar = [[[NSBundle mainBundle] loadNibNamed:@"HDBottomBarView" owner:self options:nil] lastObject];
    [_bottomView addSubview:bar];
    [bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bottomView).with.offset(0);
        make.right.equalTo(_bottomView).with.offset(0);
        make.left.equalTo(_bottomView).with.offset(0);
        make.bottom.equalTo(_bottomView).with.offset(0);
    }];
    [bar hightedBtn:0];
    bar.delegate = self;
}

- (void)barSelectAction:(NSString *)VCStr
{
    NSArray *vcArray = self.navigationController.viewControllers;
    BOOL isVCExit = NO;
    for (UIViewController *vc in vcArray) {
        if ([vc isKindOfClass:NSClassFromString(VCStr)]) {
            isVCExit = YES;
            [self.navigationController popToViewController:vc animated:NO];
            break;
        }
    }
    
    if (isVCExit == NO) {
        UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *pushVC = [board instantiateViewControllerWithIdentifier:VCStr];
        [self.navigationController pushViewController:pushVC animated:NO];
    }
}

- (void)userIDRequestAction {
    
    NSString *userID = [[NSUserDefaults standardUserDefaults] valueForKey:@"userPhoneID"];
    if (userID.length > 8) {
        [[HDDeclare sharedDeclare] setUserPhoneID:userID];
        return;
    }

    HDRequestObj *loginObj = [[HDRequestObj alloc] init];
    NSString *urlPath = [NSString stringWithFormat:@"%@g=mapi&m=Ticket&a=generate_user_login", HTTP_Request_URL_Prefix];
    NSDictionary *parameters = @{@"app_kind": @"IOS"};
    
    [loginObj requestGet:urlPath Parameter:parameters];
    loginObj.myDataBlock = ^(NSDictionary *dataDic){
        
        NSString *loginCode = dataDic[@"code"];
        if (loginCode.integerValue == 0) {
            NSDictionary *loginDic = dataDic[@"data"];
            NSString *userLogin = loginDic[@"user_login"];
            if ([userLogin isKindOfClass:[NSString class]] || userLogin.length > 0) {
                [[HDDeclare sharedDeclare] setUserPhoneID:userLogin];
            }
        }
        else {
        }
    };
}

#pragma mark
#pragma mark -------- Scan Tip Methods --------

- (void)tipImgViewInit
{
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"kTip_Image_First_Show"] == nil) {
        if (ScreenWidth == 375)
            _routeTipImgView.image = [UIImage imageNamed:@"routeVC_tip_6"];
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeTipImgView)];
        [_routeTipImgView addGestureRecognizer:myTap];
        _routeTipImgView.userInteractionEnabled = YES;
        _routeTipImgView.hidden = NO;
    }
    else
    {[self removeTipImgView];}
}

- (void)removeTipImgView
{
    [[NSUserDefaults standardUserDefaults] setValue:@"kTip_Image_First_Show" forKey:@"kTip_Image_First_Show"];
    
    [_routeTipImgView removeFromSuperview];
    _routeTipImgView = nil;
}

#pragma mark
#pragma mark -------- Tableview Methods --------

- (void)tableviewInit {
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.rowHeight = 60.0f;
    _titleArray = @[@"个人信息",@"提供服务",@"交通信息"];
    _imgNameArray = @[@"rootvc_mine",@"rootvc_service",@"rootvc_traffic"];
    _tableview.separatorColor = [UIColor clearColor];
    
    _tableview.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rootvc_bg"]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
        return 0;
    
    return 44*ScreenWidth/320;  // ----- DEBUG版本 --------
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"HDRootVCCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.textLabel.text = _titleArray[indexPath.row];
    cell.textLabel.font = [UIFont fontWithName:@"FangSong" size:15.0f];

    cell.imageView.image = [UIImage imageNamed:_imgNameArray[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1)
        [self performSegueWithIdentifier:@"rootvcPushToServiceListLine" sender:nil];
    else if (indexPath.row == 2)
        [self performSegueWithIdentifier:@"rootvcPushToTrafficListLine" sender:nil];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (IBAction)callServerAction:(id)sender {
    if ([HDSIMJudge isSIMInstalled] == NO) {
        [LCCoolHUD showFailure:@"无SIM卡" zoom:NO shadow:NO];
    }
    else {
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"022-23126808"];
        //            NSLog(@"str======%@",str);
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }
}
- (IBAction)complainAction:(id)sender {
    if ([HDSIMJudge isSIMInstalled] == NO) {
        [LCCoolHUD showFailure:@"无SIM卡" zoom:NO shadow:NO];
    }
    else {
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"022-23307222"];
    //            NSLog(@"str======%@",str);
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }
}

#pragma mark
#pragma mark -------- Dealloc Methods --------

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
